package cn.lysj.framework.foodv2.service;

import cn.lysj.framework.base.BaseService;
import cn.lysj.framework.common.domain.User;
import cn.lysj.framework.common.utils.pagination.PageUtil;
import cn.lysj.framework.common.utils.pagination.PageVo;
import cn.lysj.framework.common.utils.web.ParamUtil;
import cn.lysj.framework.common.utils.web.Resp;
import cn.lysj.framework.common.validate.annotation.Validator;
import cn.lysj.framework.foodv2.domain.Food;
import cn.lysj.framework.foodv2.domain.FoodTypeAndFoodContact;
import cn.lysj.framework.foodv2.domain.StandardLevOne;
import cn.lysj.framework.foodv2.dto.FoodListDto;
import cn.lysj.framework.foodv2.repository.FoodRepository;
import cn.lysj.framework.foodv2.repository.FoodTypeAndFoodContactRepository;
import cn.lysj.framework.foodv2.repository.StandardLevOneRepository;
import cn.lysj.framework.foodv2.repository.StandardLevTwoRepository;
import cn.lysj.framework.foodv2.vo.FoodDto;
import cn.lysj.framework.foodv2.vo.FoodListVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author drj
 * @description 支付宝用户服务层
 * @create 2018-11-09 11:20:17
 **/
@Slf4j
@Service
@Transactional
public class FoodService implements BaseService<Food> {
    @Resource
    private FoodRepository foodRepository;

    @Resource
    private FoodTypeAndFoodContactRepository foodTypeAndFoodContactRepository;

    @Resource
    private StandardLevOneRepository standardLevOneRepository;
    @Resource
    private StandardLevTwoRepository standardLevTwoRepository;

    @Override
    public FoodRepository getRepository() {
        return foodRepository;
    }

    //删除食品
    public Resp deleteRewrite(String[] ids){
        List<String> idList = Arrays.asList(ids);
        for(String id : idList){
            foodRepository.delete(id);
            foodTypeAndFoodContactRepository.deleteByFoodId(id);
            List<StandardLevOne> standardLevOnes = standardLevOneRepository.findByFoodId(id);
            for(StandardLevOne standardLevOne: standardLevOnes){
                standardLevTwoRepository.deleteByStandardLevOneId(standardLevOne.getId());
            }
            standardLevOneRepository.delete(standardLevOnes);
        }
        return new Resp("删除成功!");
    }


    //保存食品
    public Resp save(FoodDto foodDto ){
        if(ParamUtil.isBlank(foodDto.getFoodType())){
            return new Resp(Resp.PARAM_ERROR,"请选择品类信息!");
        }
        Food food = new Food();
        BeanUtils.copyProperties(foodDto,food);
        food.setActSellNum(0);
        food.setOnSale(1);//设置为在销售状态
        food.setIsUse(1);//设置为上架状态
        food.setUpdateTime(new Date());
        food = foodRepository.save(food);
        String foodId = food.getId();
        //重新构建数据
        foodTypeAndFoodContactRepository.save(createFoodTypeAndFoodContact(foodDto,foodId));
        return new Resp("添加成功!");
    }

    //更新数据
    public Resp update (FoodDto foodDto){
        if(ParamUtil.isBlank(foodDto.getFoodType())){
            return new Resp(Resp.PARAM_ERROR,"请选择品类信息!");
        }
        Food food = foodRepository.findOne(foodDto.getId());
        if(food == null){
            return new Resp("食物信息有误,编辑失败!");
        }
        food.setFoodMaterial(foodDto.getFoodMaterial());
        food.setName(foodDto.getName());
        food.setMoney(foodDto.getMoney());
        if(!ParamUtil.isBlank(foodDto.getLogo())){
            food.setLogo(foodDto.getLogo());
        }
        food.setVirtualSellNum(foodDto.getVirtualSellNum());
        food.setRemark(foodDto.getRemark());
        food.setUpdateTime(new Date());
        food = foodRepository.save(food);
        String foodId = food.getId();
        //更新数据先删除连接数据
        foodTypeAndFoodContactRepository.deleteByFoodId(foodId);
        //重新构建数据
        foodTypeAndFoodContactRepository.save(createFoodTypeAndFoodContact(foodDto,foodId));
        return new Resp("编辑成功!");
    }

    //列表显示
    public Resp list(User user , FoodListDto foodListDto , PageVo pageVo){
        Page<FoodListVo> foodListVos ;
        Pageable pageable = PageUtil.initPage(pageVo);
        if(ParamUtil.isBlank(foodListDto.getIsUse())){
            foodListDto.setIsUse("%%");
        }
        foodListVos = foodTypeAndFoodContactRepository.findFoodListOneFoodTypeId(pageable,user.getStoreId(),foodListDto.getFoodType(),foodListDto.getFoodName(),foodListDto.getIsUse());
        return new Resp(foodListVos);
    }

    //创建食物与品类关系列表信息
    public FoodTypeAndFoodContact createFoodTypeAndFoodContact(FoodDto foodDto, String foodId){
        FoodTypeAndFoodContact foodTypeAndFoodContact = new FoodTypeAndFoodContact();
        foodTypeAndFoodContact.setFoodId(foodId);
        foodTypeAndFoodContact.setFoodTypeId(foodDto.getFoodType());
        foodTypeAndFoodContact.setRank(foodDto.getRank());
        return foodTypeAndFoodContact;
    }


    public Resp foodList(User user , FoodListDto foodListDto , PageVo pageVo){
        Page<FoodListVo> foodListVos ;
        Pageable pageable = PageUtil.initPage(pageVo);
        if (ParamUtil.isBlank(foodListDto.getFoodName())){
            foodListVos = foodTypeAndFoodContactRepository.findFoodListAllFoodTypeId(pageable,user.getStoreId());
        }
        else{
            foodListVos = foodTypeAndFoodContactRepository.findFoodListAndFoodName(pageable,foodListDto.getFoodName(),user.getStoreId());
        }
        return new Resp(foodListVos);
    }
}
